<?php
/**
 * Get Allow Action from Module
 * @param type $db_conn
 * @param array $s_PPO_ModuleID
 * @return array
 */
function get_passport_action_from_module(&$db_conn, $s_PPO_ModuleID) {
    $s_UserID         = isset($_SESSION['userid']) ? $_SESSION['userid'] : '';
    $s_UserCrew       = isset($_SESSION['usercrew']) ? $_SESSION['usercrew'] : '';
    $a_allow_action   = $a_moduleactionid = array();

    $db_query = "
        select  moduleactionid, actionid
        from    passport_module_action
        where   moduleid in ('" . implode("', '", $s_PPO_ModuleID) . "')";

    $db_result = db_query($db_conn, $db_query);

    while ($d_row = db_fetch($db_result)) {
        $a_moduleactionid[$d_row->moduleactionid] = strtolower($d_row->actionid);
    }

    if (!empty($a_moduleactionid)) {

        $db_query = "
                    select  moduleactionid
                    from    passport_permission 
                    where   moduleactionid in ('" . implode("', '", array_keys($a_moduleactionid)) . "')
                            and (
                                (permissionfortype = 'GROUP' and permissionfor = '$s_UserCrew' )
                                or
                                (permissionfortype = 'USER'  and permissionfor = '$s_UserID')
                            )
                            and permissiontype = 'ALLOW'";

        $db_result = db_query($db_conn, $db_query);

        while ($d_passport_permission = db_fetch($db_result)) {

            $a_allow_action[] = $a_moduleactionid[$d_passport_permission->moduleactionid];
        }
    }

    return $a_allow_action;
}

/**
 * Get Permission from Module Action
 * @param type $db_conn
 * @param type $v_moduleactionid
 * @return boolean
 */
function namhoa_passport_with_non_target(&$db_conn, $v_moduleactionid) {
    $s_UserID   = isset($_SESSION['userid']) ? $_SESSION['userid'] : '';
    $s_UserCrew = isset($_SESSION['usercrew']) ? $_SESSION['usercrew'] : '';

    $v_has_permission = false;

    $db_query = "
                select  permissionid, permissiontype, permissionfor, permissionfortype
                from    passport_permission 
                where   moduleactionid = $v_moduleactionid
                        and (
                            (permissionfortype = 'GROUP' and permissionfor = '$s_UserCrew' )
                            or
                            (permissionfortype = 'USER'  and permissionfor = '$s_UserID')
                        )";

    $db_result = db_query($db_conn, $db_query);

    while ($d_passport_permission = db_fetch($db_result)) {
        if ($d_passport_permission->permissiontype == 'DENY') {

        } else if ($d_passport_permission->permissiontype == 'ALLOW') {
            $v_has_permission = true;
        }
    }

    return $v_has_permission;
}

/**
 * Get Permissino from Module and Action
 * @param type $db_conn
 * @param type $s_PPO_ModuleID
 * @param type $s_PPO_ActionID
 * @return boolean
 */
function get_passport_module_action(&$db_conn, &$s_PPO_ModuleID, &$s_PPO_ActionID) {
    
    $db_query = "
        select  moduleactionid
        from    passport_module_action
        where   moduleid = '$s_PPO_ModuleID' and actionid = '$s_PPO_ActionID'";

    $db_query  = $db_query . ' limit 1';
    $db_result = db_query($db_conn, $db_query);

    if (!($d_row = db_fetch($db_result))) {
        raise_error('No Action.');
    }
    
    if (!namhoa_passport_with_non_target($db_conn, $d_row->moduleactionid)) {
        raise_error('Bạn chưa được cấp quyền thực hiện chức năng này.');
    }

    return true;
}

/**
 * Get Hidden Page from Passport
 * @param type $db_conn
 * @param type $a_PPO_Pages
 * @return array
 */
function get_passport_hidden_page($db_conn, $a_PPO_Pages) {
    $s_UserID      = isset($_SESSION['userid']) ? $_SESSION['userid'] : '';
    $s_UserCrew    = isset($_SESSION['usercrew']) ? $_SESSION['usercrew'] : '';
    $a_hidden_page = array();
    $a_tmp_module  = array();

    $db_query = "
                select  moduleactionid, permissiontype
                from    passport_permission 
                where   (
                            (permissionfortype = 'GROUP' and permissionfor = '$s_UserCrew' )
                            or
                            (permissionfortype = 'USER'  and permissionfor = '$s_UserID')
                        )";

    $db_result = db_query($db_conn, $db_query);

    while ($d_passport_permission = db_fetch($db_result)) {
        if ($d_passport_permission->permissiontype == 'ALLOW') {
            $a_tmp_module[$d_passport_permission->moduleactionid] = $d_passport_permission->moduleactionid;
        }
    }

    $db_query = "
                select  moduleid
                from    passport_module_action
                where   moduleactionid in ('" . implode("', '", $a_tmp_module) . "')";

    $db_result = db_query($db_conn, $db_query);

    $a_tmp_module = array();

    while ($d_passport_module_action = db_fetch($db_result)) {
        $a_tmp_module[$d_passport_module_action->moduleid] = $d_passport_module_action->moduleid;
    }

    $db_query = "
                select  pageid
                from    passport_module_page 
                where   moduleid in ('" . implode("', '", $a_tmp_module) . "')";

    $db_result = db_query($db_conn, $db_query);

    while ($d_passport_module_page = db_fetch($db_result)) {

        $a_hidden_page[] = $d_passport_module_page->pageid;
    }

    $a_hidden_page = array_diff($a_PPO_Pages, $a_hidden_page);

    if (!empty($a_hidden_page)) {
        $a_hidden_page = array_map(function ($value) {
                return strtolower($value) . '.php';
            }, $a_hidden_page);
    }

    return $a_hidden_page;
}
?>